#include <iostream>
#include <string>
#include <vector>
#include <memory>

// 1. 中介者接口
class Mediator {

public:
    virtual ~Mediator() = default;
    virtual void sendMessage(const std::string& message, const std::string& sender) = 0;
};

// 2. 同事类接口
class Colleague {

protected:
    Mediator* mediator_;
    std::string name_;

public:
    Colleague(Mediator* mediator, const std::string& name):mediator_(mediator), name_(name) {}

    virtual ~Colleague() = default;

    std::string getName() const {
        return name_;
    }

    virtual void receiveMessage(const std::string& message, const std::string& sender) {
        std::cout << "[" << name_ << "] received message from [" << sender << "]: " << message << "\n";
}

    void sendMessage(const std::string& message) {
            mediator_->sendMessage(message, name_);
        }
};

// 3. 具体中介者
class ChatMediator : public Mediator {

private:
    std::vector<std::shared_ptr<Colleague>> colleagues_;

public:
    void addUser(const std::shared_ptr<Colleague>& user) {
            colleagues_.push_back(user);
    }

    void sendMessage(const std::string& message, const std::string& sender) override 
    {
        for (const auto& colleague : colleagues_) 
        {
            if (colleague->getName() != sender) 
            {
                colleague->receiveMessage(message, sender);
            }
        }
    }
};

// 4. 客户端代码
int main() {
    
    ChatMediator chatRoom;

    auto user1 = std::make_shared<Colleague>(&chatRoom, "Alice");
    auto user2 = std::make_shared<Colleague>(&chatRoom, "Bob");
    auto user3 = std::make_shared<Colleague>(&chatRoom, "Charlie");

    chatRoom.addUser(user1);
    chatRoom.addUser(user2);
    chatRoom.addUser(user3);

    user1->sendMessage("Hello, everyone!");
    user2->sendMessage("Hi, Alice!");
    user3->sendMessage("Good morning!");

    return 0;
}